using B.S.RbacData.ErrorCode;
using ContentmanagementBaseWrite.API.Applications.Commands.Departments;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseWrite.API.Controllers
{
    /// <summary>
    /// 科室管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<DepartmentController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public DepartmentController(IMediator mediator, ILogger<DepartmentController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增科室
        /// </summary>
        /// <param name="addDepartmentDto">新增科室DTO</param>
        /// <returns>新增结果</returns>
        [HttpPost("add")]
        public async Task<ApiResult<int>> AddDepartment([FromBody]AddNewDepartmentCommand command)
        {
            try
            {
                _logger.LogInformation("接收到新增科室请求，科室名称：{DepartmentName}", command.DepartmentName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("科室新增请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "科室新增请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"科室新增请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 更新科室
        /// </summary>
        /// <param name="command">更新科室命令</param>
        /// <returns>更新结果</returns>
        [HttpPut("update")]
        public async Task<ApiResult<int>> UpdateDepartment([FromBody] UpdateDepartmentCommand command)
        {
            try
            {
                _logger.LogInformation("接收到更新科室请求，科室ID：{Id}，科室名称：{DepartmentName}", command.Id, command.DepartmentName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("科室更新请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "科室更新请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"科室更新请求处理异常：{ex.Message}",
                    data = 0
                                 };
             }
         }

         /// <summary>
         /// 批量删除科室
         /// </summary>
         /// <param name="command">批量删除科室命令</param>
         /// <returns>删除结果</returns>
         [HttpDelete("batch-delete")]
         public async Task<ApiResult<int>> BatchDeleteDepartment([FromBody] BatchDeleteDepartmentCommand command)
         {
             try
             {
                 _logger.LogInformation("接收到批量删除科室请求，科室ID列表：{DepartmentIds}", string.Join(",", command.DepartmentIds));

                 var result = await _mediator.Send(command);

                 _logger.LogInformation("科室批量删除请求处理完成，结果：{Result}", result.message);

                 return result;
             }
             catch (Exception ex)
             {
                 _logger.LogError(ex, "科室批量删除请求处理异常");
                 return new ApiResult<int>
                 {
                     code = ResultEnumCode.fails,
                     message = $"科室批量删除请求处理异常：{ex.Message}",
                     data = 0
                 };
             }
         }
     }
 } 